<template>
  <div class="app-container">
    <!--用户数据-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称">
        <el-input v-model="queryParams.cleanerName" placeholder="请输入名称" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职责">
        <el-select v-model="queryParams.cleanerType" placeholder="请选择职责" clearable style="width: 240px">
          <el-option v-for="item in cleanerTypeList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['enterprise:cleaner:add']"
        >新增
        </el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column label="名称" align="center" key="cleanerName" prop="cleanerName" :show-overflow-tooltip="true"/>
      <el-table-column label="职责" align="center" key="cleanerContent" prop="cleanerContent"/>
      <el-table-column label="职责范围" align="center" key="estateNumber" prop="estateNumber">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            width="400"
            trigger="click"
            v-if="scope.row.estateNumber > 0"
          >
            <div  class="estate-popover">
              <div class="item" v-for="(item,index) in scope.row.estateNames.split(',')" :key="item">
                <div class="label">
                  <span>{{ index+1 }}</span>
                  <span>{{ item }}</span>
                </div>
              </div>
            </div>
            <el-button type="text"  slot="reference">{{ scope.row.estateNumber }}</el-button>
          </el-popover>
          <span v-else>全部</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.userId !== 1">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['enterprise:cleaner:edit']"
          >编辑
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['enterprise:cleaner:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
    <add v-model="open" :title="title" @success="getList" :data="form"/>
  </div>
</template>

<script>
import add from './add.vue'
import { listCleaner, getCleaner, deleteCleaner } from '@/api/houseCleaning/cleaner.js'

export default {
  name: 'Index',
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: { add },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      //用户id
      userId: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `状态`, visible: true },
        { key: 6, label: `创建时间`, visible: true }
      ],
      cleanerTypeList: [
        { label: '验房', value: 1 },
        { label: '打扫', value: 2 }
      ]
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listCleaner(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        danhao: undefined,
        fangjian: undefined,
        fenlei: undefined,
        status: undefined,
        yanfangyuan: undefined
      },
        this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = undefined
      this.open = true
      this.title = '新增人员'
    },
    // 更新操作
    handleUpdate(row) {
      getCleaner(row.cleanerId).then(res => {
        this.form = res.data
        this.title = '编辑人员'
        this.open = true
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.cleanerId || this.ids
      this.$modal.confirm('确定删除数据？').then(function() {
        return deleteCleaner(Ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.estate-popover{
  max-height: 200px;
  overflow-y: auto;
  .item {
    padding: 10px;
    border-bottom: 1px solid #f0f0f0;
    &:last-child {
      border-bottom: none;
    }
    .label {
      display: flex;
      align-items: center;
      span:first-child {
        margin-right: 10px;
        border: 1px solid;
        border-radius: 50%;
        width: 15px;
        height: 15px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>
